
import { _decorator, Node, Sprite, Label } from 'cc';
import yyComponent from '../../Script/Common/yyComponent';
import { GlobalEnum } from '../../Script/GameSpecial/GlobalEnum';
import Loader from '../../Script/Common/Loader';
import { EventType } from '../../Script/GameSpecial/GameEventType';
const { ccclass, property } = _decorator;

@ccclass('TeachAnim')
export class TeachAnim extends yyComponent {

    protected initSub() {
        this.hideControlTip();
        this.hideTeachTip();
        this.hideTouchMask();
        this.awardUI.active = false;
    }
    protected onEvents() {
        this.on(EventType.myTeachEvent.showTeachTip, this.showTeachTip, this);
        this.on(EventType.myTeachEvent.showTeachAward, this.showAward, this);
    }

    @property(Node)
    protected touchMask: Node = null;
    @property(Sprite)
    protected weaponImg: Sprite = null;
    @property(Label)
    protected weaponTip: Label = null;
    @property(Node)
    protected btnOK: Node = null;
    @property(Node)
    protected controlTip: Node = null;
    @property(Node)
    protected portal: Node = null;

    @property(Node)
    protected awardUI: Node = null;
    protected gold: number = 100;
    public showAward() {
        this.hideControlTip();
        this.hideTeachTip();
        this.touchMask.active = true;
        this.awardUI.active = true;
        this.showUI(GlobalEnum.UI.playerAssetBar);
    }
    protected onBtnGetAward() {
        this.playClickBtn();
        this.emit(EventType.Common.UIEvent.playGoldAnim, {
            cb: () => {
                this.emit(EventType.Common.PlayerDataEvent.updatePlayerData, {
                    attribute: "gameData.asset.gold",
                    value: this.gold,
                    mode: "+",
                    emit: true,
                    save: true,
                });
                this.emit(EventType.myTeachEvent.gotTeachAward);
            },
            gold: this.gold,
        });
    }

    public showTeachTip(data: { showPortal?: boolean, weaponImg?: string, str?: string }) {
        if (undefined !== data.weaponImg) {
            this.setWeaponImg(data.weaponImg);
        }
        if (undefined !== data.str) {
            this.setWeaponTip(data.str);
        }
        if (!!data.showPortal) {
            this.portal.parent.active = true;
            this.portal.active = true;
        }

        this.btnOK.active = true;
        this.touchMask.active = true;
        this.hideControlTip();
    }
    public showControlTip() {
        this.controlTip.parent.active = true;
    }
    public hideControlTip() {
        this.controlTip.parent.active = false;
    }
    public hideTeachTip() {
        this.weaponImg.node.active = false;
        this.weaponImg.node.parent.active = false;
        this.weaponTip.node.parent.active = false;
        this.portal.active = false;
        this.btnOK.active = false;
        this.touchMask.active = false;
    }

    public setWeaponImg(img: string) {
        Loader.loadBundleRes("Teach", "Image/" + img + "/spriteFrame", (res) => {
            this.weaponImg.node.parent.active = true;
            this.weaponImg.node.active = true;
            this.weaponImg.spriteFrame = res;
        }, true);
    }
    public setWeaponTip(str: string) {
        this.weaponTip.node.parent.active = true;
        this.weaponTip.string = str;
    }


    protected onBtnOK() {
        this.playClickBtn();
        this.hideTeachTip();
        this.showControlTip();
        this.emit(EventType.myTeachEvent.decideTip);
    }

}
